home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2002 November / SGI IRIX 6.5 Applications 2002 November.iso / dist / gateway.idb / usr / WebFace / Source / 10-InfoSystems / mail / sendmail-conf.frm.z / sendmail-conf.frm
Encoding:
Text File  |  2002-06-12  |  11.3 KB  |  334 lines

  1. #! /usr/bin/perl5
  2. #
  3. # sendmail-conf.cgi
  4. #
  5. # Copyright 1988-1996 Silicon Graphics, Inc.
  6. # All rights reserved.
  7. #
  8. # This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  9. # the contents of this file may not be disclosed to third parties, copied or
  10. # duplicated in any form, in whole or in part, without the prior written
  11. # permission of Silicon Graphics, Inc.
  12. #
  13. # RESTRICTED RIGHTS LEGEND:
  14. # Use, duplication or disclosure by the Government is subject to restrictions
  15. # as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  16. # and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  17. # successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  18. # rights reserved under the Copyright Laws of the United States.
  19. #
  20. # $Id: sendmail-conf.frm,v 1.26 1997/11/17 19:08:13 shotes Exp $
  21.  
  22. BEGIN { require "/usr/WebFace/lib/CGI.pm"; import CGI; }
  23. require "/usr/OnRamp/lib/OnRamp.pm";
  24. require "/usr/OnRamp/lib/java.pm";
  25. require "flush.pl";
  26.  
  27. $query = new CGI;
  28.  
  29. $help_page = "sendmail-conf-help.html";
  30. $cmd = "/usr/etc/configmail";
  31. $prm = "/etc/sendmail.params";
  32. $tmpprm = "/etc/sendmail.params.tmp";
  33. $MAILSCRIPT = "/etc/init.d/mail";
  34. $title = "Sendmail Configuration";
  35.  
  36. if ($ENV{'HTTP_USER_AGENT'} =~ /Mozilla\/2/) { $br_index = 1; }
  37. else { $br_index = 0; }
  38.  
  39. $js = 
  40. "$js_check_int
  41. $js_help
  42. which = \"none\";
  43. br_index = $br_index;
  44. function runSubmit() {
  45.     // onClick gets processed before onSubmit
  46.     if(which == \"ok\") return runOK();
  47.     return (true);
  48. }
  49. function markOK() { which = \"ok\"; }
  50. function markOther() { which = \"none\"; }
  51. function runOK() {
  52.     form = document.SendmailForm;
  53.     if (form.enable[br_index].checked) { // radio buttons are backwards
  54.         if (!testHostname(form.forwarder,\"forwarding host name\")) return (false);
  55.         if (!testHostname(form.rootdomain,\"root domain name\")) return (false);
  56.         if (!testHostname(form.relayname,\"relay host name\")) return (false);
  57.         if (!testHostname(form.localdomain,\"local domain name\")) return (false);
  58.         if (!testDirectory(form.pathalias)) return (false);
  59.         if (!testHostList(form.directdomains)) return (false);
  60.     } return (true);
  61. }
  62. function testDirectory(Ctrl) {
  63.     word = Ctrl.value;
  64.     if (word == \"\") { errorBox (Ctrl, \"The UUCP path-alias directory is required.\\nTo disable the path-alias database, enter \\n/dev/null as the directory.\"); Ctrl.value = \"/dev/null\"; return (false); } 
  65.     for(i = 0; i < word.length; i++) {
  66.         if (word.charAt(i) == ' ') { errorBox (Ctrl, \"The UUCP directory cannot \\ncontain spaces.\"); return (false); }
  67.     }
  68.     if (word.charAt(0) != '/') { errorBox (Ctrl, \"The UUCP directory must be \\nfully qualified.\"); return (false); }
  69.     return (true);
  70. }
  71. function testHostname(Ctrl,what) {
  72.     if (Ctrl.value == \"\") { errorBox (Ctrl, \"The \" + what + \" is required.\"); return (false); } 
  73.     error = testHost(Ctrl.value);
  74.     if (error == 1) { errorBox (Ctrl, \"The \" + what + \" cannot \\ncontain \" + illegal + \" characters.\"); return (false); }
  75.     if (error == 2) { errorBox (Ctrl, \"The \" + what + \" cannot start or \\n end with a . or - or _ character.\"); return (false); }
  76.     if (error == 3) { errorBox (Ctrl, \"The \" + what + \" cannot contain \\nconsecutive . or - or _ characters.\"); return (false); }
  77.     if (error == 4) { errorBox (Ctrl, \"The \" + what + \" cannot contain spaces.\"); return (false); }
  78.     if (error == 5) { errorBox (Ctrl, \"The \" + what + \" cannot begin \\nwith a number.\"); return (false); }
  79.     return (true);
  80. }
  81. function testHostList(Ctrl) {
  82.     whitespace = \" \\n\\r\\t\\f\";  space = true;
  83.     for(start=0, cur=0; cur < Ctrl.value.length; cur++) {
  84.         for(i = 0; i < whitespace.length; i++) {
  85.             c = whitespace.charAt(i);
  86.             if (Ctrl.value.charAt(cur) == c) { // found whitespace
  87.                 if (space == false) { space = true;  if(cur != start) { host = Ctrl.value.substring(start,cur); if (testHost(host)) { errorBox (Ctrl, \"Invalid relay host name: \" + host); return (false); } } } break;
  88.             } else {                           // found character 
  89.                 if (space == true) { space = false;  start = cur; }
  90.             }
  91.         }
  92.     }
  93.     if(cur != start && space == false) { host = Ctrl.value.substring(start,cur); if (testHost(host)) { errorBox (Ctrl, \"Invalid relay host name: \" + host); return (false); } }
  94.     return (true);
  95. }
  96. function testHost(word) {
  97.     metaChars = \"{}()*!~<>?|[]'&^\$\@\#`\\\";:\\\\/+=,%\";
  98.     start = 0;  while (word.charAt(start) == ' ') { start++; }
  99.     end = word.length-1; while (word.charAt(end) == ' ') { end--; }
  100.     for(j = 0; j < metaChars.length; j++) {
  101.         c = metaChars.charAt(j); i = word.indexOf(c, 0); if (i != -1) { illegal = c; return 1; }
  102.     }
  103.     sch = word.charAt(start); ech = word.charAt(end);
  104.     if(sch == '.' || sch == '-' || sch == '_') return 2;
  105.     if(ech == '.' || ech == '-' || ech == '_') return 2;
  106.     for(j = start; j <= end; j++) { 
  107.     ch = word.charAt(j); 
  108.     if (ch == '.' || ch == '-' || ch == '_') { 
  109.         if (word.charAt(j+1) == ch) return 3; 
  110.     } 
  111.     if (ch == ' ') { return 4; } }
  112.     if(checkInt_c(sch, 10)) return 5;
  113.     return 0;
  114. }
  115. function errorBox (Ctrl, ErrorMessage) {
  116.     alert (ErrorMessage);  Ctrl.focus();  return;
  117. }";
  118.  
  119. @supported = (    'forwarder',
  120.         'rootdomain',
  121.         'relayname',
  122.         'localdomain',
  123.         'pathalias',
  124.         'directdomains' );
  125.  
  126. print $query->header;
  127.  
  128. &getValues;
  129.  
  130. if ($query->param) {
  131.     $help = $document_root . $ENV{"SCRIPT_NAME"};
  132.     $help =~ s/cgi$/hlp/;
  133.     exec $help if ($query->param('help') eq "Help");
  134.  
  135.     if ($query->param('doit') eq 'Ok') {
  136.     if ($query->param('enable') eq 'Yes') { &formValidation; }
  137.  
  138.     if ($hash{'enable'} ne $query->param('enable')) {
  139.         if ($query->param('enable') eq 'Yes') {
  140.     system("/etc/chkconfig", "sendmail", "on");
  141.         $message = "Sendmail enabled.";
  142.         } else { 
  143.     system("/etc/chkconfig", "sendmail", "off");
  144.         $message = "Sendmail disabled."; 
  145.         }
  146.         &flush(stdout);
  147.         if (!fork) {
  148.     system($MAILSCRIPT, "stop");
  149.     system($MAILSCRIPT, "start");
  150.         exit 0;
  151.         }
  152.     }
  153.  
  154.     if ($query->param('enable') eq 'Yes') {
  155.         undef %fileList;
  156.         open(PRM, "< $prm");
  157.         while(<PRM>) {
  158.         chomp();
  159.         ($left,$right) = split(/:/);
  160.             if ($left && $right) {
  161.         $fileList{$left} = $right;
  162.             }
  163.         }
  164.         close(PRM);
  165.  
  166.         $mod = 0;
  167.  
  168.         foreach $arg (@supported) { $vals{$arg} = $query->param($arg) }
  169.  
  170.         foreach $arg (keys(%vals)) {
  171.         if ($vals{$arg} ne $hash{$arg}) {
  172.             $mod = 1;
  173.             $fileList{$arg} = $vals{$arg};
  174.         }
  175.         }
  176.  
  177.         if ($mod == 1) {
  178.         open(PRMT, ">$tmpprm");
  179.         foreach $arg (keys(%fileList)) {
  180.             print PRMT "$arg:$fileList{$arg}\n";
  181.         }
  182.         close(PRMT);
  183.         rename($tmpprm, $prm);
  184.     system($MAILSCRIPT, "start");
  185.  
  186.         $message .= "  Configuration file updated.";
  187.         }
  188.     }
  189.     }
  190.     if (!$message) { $message = "No changes made."; }
  191. }
  192.  
  193. $NSstatus = &checkForNS;
  194.  
  195. if ($NSstatus) { 
  196.     &title_block($title);
  197.     &header_block($title);
  198.     print "<i>NS Mail is currently running.  To enable sendmail, first
  199.         disable NS Mail.</i>";
  200. } else { &generic; }
  201.  
  202. exit 0;
  203.  
  204. sub checkForNS {
  205.     local($mail_process) = "NscpMail";
  206.     local($ret) = 0;
  207.     open(PSNS, "/bin/ps -ef |");
  208.     while(<PSNS>) {
  209.     if ($_ =~ m:$mail_process:) { $ret = 1; }
  210.     }
  211.     close(PSNS);
  212.     $ret;
  213. }
  214.  
  215. sub error {
  216.     &error_block($_[0]);
  217.     &generic;
  218.     exit 0;
  219. }
  220.  
  221. sub getValues {
  222.     $hash{'enable'} = &get_config("sendmail");
  223.    
  224.     open(LIST, "$cmd list |");
  225.     @configlist = <LIST>;
  226.     close(LIST);
  227.  
  228.     for ($i = 0; $i < $#configlist; $i++) {
  229.     $configlist[$i] =~ s/[\"\s+]//g;
  230.     ($attr, $aval) = split(/=/, $configlist[$i]);
  231.     if ($attr ne "") {
  232.         $hash{$attr} = $aval; chop($aval);
  233.     }
  234.     }
  235.     $hash{'directdomains'} =~ s/,/\n/g;
  236. }
  237.  
  238. sub formValidation {
  239.     my $error;
  240.  
  241.     $error = &check_hostname($query->param('forwarder'));
  242.     &error("Forwarding host name cannot contain shell metacharacters") if $error == 911;
  243.     &error("Invalid forwarding host name") if $error;
  244.  
  245.     $localDomain = $query->param('localdomain');
  246.     &error("Invalid local domain name.") 
  247.         if (!$localDomain || &check_hostname($localDomain));     
  248.  
  249.     $error = &check_hostname($query->param('rootdomain'));
  250.     &error("Root domain name cannot contain shell metacharacters") if $error == 911;
  251.     &error("Invalid root domain name") if $error;
  252.  
  253.     my $directs = $query->param('directdomains');
  254.     $directs =~ s/[\n\s]+/,/g;
  255.     $directs =~ s/(^,)+|(,$)//g;
  256.     foreach (split(/,/,$directs)) {
  257.         &error(qq|Invalid relay hostname "$_"|) if &check_hostname($_);
  258.     }
  259.  
  260.     $query->param('directdomains', $directs);
  261.  
  262.     &error("UUCP path-alias database directory must already exist") if 
  263.         ! -d $query->param('pathalias') && $query->param('pathalias') ne '/dev/null';
  264.  
  265.     $error = &check_hostname($query->param('relayname'));
  266.     &error("Relay host name cannot contain shell metacharacters") if $error == 911;
  267.     &error("Invalid relay host name") if $error;
  268. }
  269.  
  270. sub generic {
  271.     &js_title_block($title,$js);
  272.     &header_block($title);
  273.  
  274.     print "<i>$message</i>";
  275.  
  276.     print $query->startform("POST", "", "", "NAME=SendmailForm", "onSubmit=\"return runSubmit(this.form)\"");
  277.  
  278.     print "<center><table cellpadding=5 width=450>\n";
  279.  
  280.     print "<tr><th align=left>Enable sendmail:</th><th align=left>",
  281.         $query->radio_group(-name=>'enable', -values=>['Yes','No'],
  282.                     -default=>$hash{'enable'}),"</th></tr>";
  283.  
  284.     print "<tr><th align=left>Hostname of mail forwarder:</th>\n",
  285.         "<th align=left>",
  286.         $query->textfield(-name=>'forwarder',
  287.                    -default=>$hash{"forwarder"},
  288.                    -size=>20, -maxlength=>256), "</th></tr>";
  289.  
  290.     print "<tr><th align=left>Root domain name:</th>\n",
  291.         "<th align=left>",
  292.         $query->textfield(-name=>'rootdomain',
  293.                    -default=>$hash{"rootdomain"},
  294.                    -size=>20, -maxlength=>256), "</th></tr>";
  295.  
  296.     print "<tr><th align=left>Hostname of mail relay:</th>\n",
  297.         "<th align=left>",
  298.         $query->textfield(-name=>'relayname',
  299.                    -default=>$hash{"relayname"},
  300.                    -size=>20, -maxlength=>256), "</th></tr>";
  301.  
  302.     print "<tr><th align=left>Local domain name:</th>\n",
  303.         "<th align=left>",
  304.         $query->textfield(-name=>'localdomain',
  305.                    -default=>$hash{"localdomain"},
  306.                    -size=>20, -maxlength=>256), "</th></tr>";
  307.  
  308.     print "<tr><th align=left>Directory of the UUCP path-alias database:</th>\n",
  309.         "<th align=left>",
  310.         $query->textfield(-name=>'pathalias',
  311.                    -default=>$hash{"pathalias"},
  312.                    -size=>20, -maxlength=>256), "</th></tr>";
  313.  
  314.     print "<tr><th align=left valign=top>List of domains for which this host
  315.                   is the relay:</th>\n",
  316.         "<th align=left>";
  317.  
  318.     $fix = $query->param('directdomains');
  319.     $fix =~ s/,/\n/g;
  320.     $query->param('directdomains',$fix);
  321.  
  322.     print $query->textarea(-name=>'directdomains', -rows=>4,
  323.                      -default=>$hash{"directdomains"}),
  324.                     "</th></tr>";
  325.  
  326.     print "</table></center><br>";
  327.  
  328.     print &js_buttons('doit','Ok','onClick="markOK()"','onClick="markOther()"',
  329.         "onClick=\"do_help('$help_page'); return (false)\"");
  330.  
  331.     print $query->endform;
  332.  
  333. }
  334.